package com.huaqin.excel.handler;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class TxtDataHandler {

	/**
	 * delete files
	 * 
	 * @param fileName
	 * @return
	 */
	public static boolean deleteFile(String filePath) throws Exception {
		boolean flag = false;
		try {
			File fileName = new File(filePath);
			if (fileName.exists()) {
				fileName.delete();
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	// write to txt not update
	public static void contentToTxt(String filePath, String content) {
		String str = new String(); // old content
		String s1 = new String();// new content
		try {
			File f = new File(filePath);
			if (f.exists()) {
				System.out.print("file exsist");
			} else {
				System.out.print("file not exsist");
				f.createNewFile();// if not exsist,create
			}
			BufferedReader input = new BufferedReader(new FileReader(f));

			while ((str = input.readLine()) != null) {
				s1 += str + "\n";
			}
			System.out.println(s1);
			input.close();
			s1 += content;

			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(s1);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

}